package com.roger.query.service.impl;

import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;

import com.roger.query.service.QueryService;
import com.roger.vo.RangeQueryParams;

/**
 * 范围查询
 * @author luoweiyi
 */
public class RangeQueryServiceImpl<T> implements QueryService<RangeQueryParams<T>> {

	@Override
	public Query getQuery(RangeQueryParams<T> params) {
		Query query = null;

		T start = params.getStart();
		T end = params.getEnd();

		if (null == start && null == end) return null;

		T temp = null != start ? start : end;
		if (temp instanceof String) {
			query = TermRangeQuery.newStringRange(params.getField(), (String) start, (String) end, params.isIncludeLower(), params.isIncludeUpper());
		} else if (temp instanceof Integer) {
			query = IntPoint.newRangeQuery(params.getField(), (Integer) start, (Integer) end);
		} else if (temp instanceof Long) {
			query = LongPoint.newRangeQuery(params.getField(), (Long) start, (Long) end);
		} else if (temp instanceof Float) {
			query = FloatPoint.newRangeQuery(params.getField(), (Float) start, (Float) end);
		} else if (temp instanceof Double) {
			query = DoublePoint.newRangeQuery(params.getField(), (Double) start, (Double) end);
		} else {
			System.out.println("The params type is error!!!");
		}
		return query;
	}

}
